//
//  UIBarButtonItem+XExtension.m
//  XWeibo-OC
//
//  Created by Apple on 15/10/25.
//  Copyright © 2015年 Apple. All rights reserved.
//

#import "UIBarButtonItem+XExtension.h"

@implementation UIBarButtonItem (XExtension)

+ (instancetype)barButtonItemWithTitle:(NSString *)title addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
{
    UIButton *btn = [[UIButton alloc] init];
    
    CGSize btnSize = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
    
    btn.bounds = CGRectMake(0, 0, btnSize.width, btnSize.width);
    
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [btn setTitle:title forState:UIControlStateNormal];
    
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    
    //添加监听事件
    [btn addTarget:target action:action forControlEvents:controlEvents];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    return item;
    
}


@end
